﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Forms;
using System.Linq;

namespace TestDxfDocument
{
    class ToolShanksColletsNuts// 工具柄用安装锥的弹簧夹头弹簀夹头、螺母和配合尺寸
    {
        public static void ToolShanksCollets(Dictionary<string, Object> data)//弹簧夹头
        {
            DxfDocument dxf = new DxfDocument();

            #region 图层

            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层

            #endregion

            #region 参数

            double pi = Math.PI;
            double rad = pi / 180;
            double offset = 10;

            string path = Convert.ToString(data["path"]);
            double scale = Convert.ToDouble(data["scale"]);
            //double scale = 2;

            /*double d1 = 12.35 * scale;
            double d2 = 52.95 * scale;
            double d3 = 51.55 * scale;
            double d4 = 25.54 * scale;
            double d5 = 46.75 * scale;
            double d6 = 52.82 * scale;

            double l1 = 63.81 * scale;
            double l2 = 10.79 * scale;
            double l3 = 5.23 * scale;
            double l4 = 8.51 * scale;
            double l5 = 24.61 * scale;

            string ta = "0";
            string colst = "B";*/

            double d1 = Convert.ToDouble(data["dconws"]) * scale;
            double d2 = Convert.ToDouble(data["dfc"]) * scale;
            double d3 = Convert.ToDouble(data["bd"]) * scale;
            double d4 = Convert.ToDouble(data["dcob"]) * scale;
            double d5 = Convert.ToDouble(data["dgui"]) * scale;
            double d6 = Convert.ToDouble(data["bd"]) * scale;

            double l1 = Convert.ToDouble(data["lcol"]) * scale;
            double l2 = Convert.ToDouble(data["ldg"]) * scale;
            double l3 = Convert.ToDouble(data["gw"]) * scale;
            double l4 = Convert.ToDouble(data["lfsf"]) * scale;
            double l5 = Convert.ToDouble(data["lcob"]) * scale;

            string ta = Convert.ToString(data["ta"]);
            string colst = Convert.ToString(data["colst"]);//夹头类型


            double d2s = d2 / 52.95;
            double d3s = d3 / 51.55;
            double l4s = l4 / 8.51;

            double ang = (ta == "0") ? 8 * rad : Math.Atan(1.0 / 10);//锥度
            int arrayCount = (colst == "A") ? 3 : 6;//阵列个数

            #endregion

            #region 点

            //右图
            Vector2 v0 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(v0.X, v0.Y - d3s * 0.87);
            Vector2 v2 = new Vector2(v0.X, v0.Y - d1 / 2);
            Vector2 v3 = new Vector2(v0.X, v0.Y - d3s * 10.95);
            Vector2 v4 = new Vector2(v0.X, v3.Y - d3s * 1.6);
            Vector2 v5 = new Vector2(v0.X, v0.Y - d3s * 18.8);
            Vector2 v6 = new Vector2(v0.X, v5.Y - d3s * 1.4);
            Vector2 v8 = new Vector2(v0.X - l4 * 5 / 6, v1.Y);
            Vector2 v9 = new Vector2(v0.X - l4 * 5 / 6, v0.Y - d3s * 12.7);
            Vector2 v10 = new Vector2(v0.X - l4 * 5 / 6, v9.Y - d3s * 1.6);
            Vector2 v11 = new Vector2(v0.X - l4 * 5 / 6, v0.Y - d3s * 22.34);
            Vector2 v12 = new Vector2(v0.X - l4 * 5 / 6, v11.Y - d3s * 1.4);
            Vector2 v13 = new Vector2(v0.X - l4 * 5 / 6, v0.Y - d3 / 2);
            Vector2 v7 = new Vector2(v0.X, v13.Y + (v1.X - v13.X) * Math.Tan(30 * rad));
            Vector2 v14 = new Vector2(v0.X - l4, v1.Y);
            Vector2 v15 = new Vector2(v0.X - l4, v9.Y + d3s * 1.6);
            Vector2 v16 = new Vector2(v0.X - l4, v9.Y);
            Vector2 v17 = new Vector2(v0.X - l4, v10.Y);
            Vector2 v18 = new Vector2(v0.X - l4, v11.Y + d3s * 1.4);
            Vector2 v19 = new Vector2(v0.X - l4, v11.Y);
            Vector2 v20 = new Vector2(v0.X - l4, v12.Y);
            Vector2 v21 = new Vector2(v0.X - l4, v0.Y - d6 / 2);
            Vector2 v22 = new Vector2(v0.X - l4 - l3, v1.Y);
            Vector2 v23 = new Vector2(v22.X, v15.Y);
            Vector2 v24 = new Vector2(v22.X, v16.Y);
            Vector2 v25 = new Vector2(v22.X, v17.Y);
            Vector2 v26 = new Vector2(v22.X, v18.Y);
            Vector2 v27 = new Vector2(v22.X, v19.Y);
            Vector2 v28 = new Vector2(v22.X, v0.Y - d2s * 23.54);
            Vector2 v29 = new Vector2(v22.X, v28.Y - d2s * 1.4);
            Vector2 v31 = new Vector2(v0.X - l2 - l4, v0.Y - d2 / 2);
            Vector2 v30 = new Vector2(v22.X, v31.Y - (l2 - l3) * Math.Tan(ang));
            Vector2 v33 = new Vector2(v0.X - (l1 - l5), v0.Y - d4 / 2);
            Vector2 v32 = new Vector2(v33.X + (d4 - d1) / 2 * Math.Tan(30 * rad), v2.Y);
            Vector2 v36 = new Vector2(v0.X - l1, v1.Y);
            Vector2 v34 = new Vector2(v36.X + l4s * 1.6, v0.Y - d2s * 9.04);
            Vector2 v35 = new Vector2(v34.X, v34.Y - d2s * 1.6);
            Vector2 v37 = new Vector2(v0.X - l1, v33.Y);
            Vector2 v38 = new Vector2(v0.X - l1, v0.Y - d2s * 16.4);
            Vector2 v39 = new Vector2(v0.X - l1, v38.Y - d2s * 1.4);
            Vector2 v40 = new Vector2(v0.X - l1, v31.Y + (l1 - l2 - l4) * Math.Tan(ang));

            if (arrayCount == 3)
            {
                v19 = v11;
                v20 = v12;
            }

            //左图

            Vector2 v100 = Vector2.Polar(v0, l1 + d6 * 1.5, pi);
            Vector2 v101 = Vector2.Polar(v100, d1 / 2, pi / 2 - (Math.Asin(d3s * 0.9 / (d1 / 2))));
            Vector2 v102 = Vector2.Polar(v101, (d3 - d1) / 8, pi / 2);
            Vector2 v103 = Vector2.Polar(v101, (d3 - d1) / 4, pi / 2);
            Vector2 v104 = Vector2.Polar(v100, (v0 - v7).Y, pi / 2 - Math.Asin(d3s * 0.9 / (v0 - v7).Y));
            Vector2 v105 = Vector2.Polar(v100, d3 / 2, pi / 2 - Math.Asin(d3s * 0.9 / (d3 / 2)));
            Vector2 v106 = Vector2.Polar(v100, d1 / 2 + (v104 - v101).Y / 2, pi / 2 - (2 * pi / arrayCount / 2) + Math.Asin(d3s * 0.9 / (d1 / 2 + (v104 - v101).Y / 2)));
            Vector2 v107 = Vector2.Polar(v100, (v0 - v7).Y, pi / 2 - (2 * pi / arrayCount / 2) + Math.Asin(d3s * 0.9 / (v0 - v7).Y));
            Vector2 v108 = Vector2.Polar(v100, d3 / 2, pi / 2 - (2 * pi / arrayCount / 2) + Math.Asin(d3s * 0.9 / (d3 / 2)));
            Vector2 v109 = Vector2.Polar(v100, d1 / 2 + (v104 - v101).Y / 2, pi / 2 - (2 * pi / arrayCount / 2) - Math.Asin(d3s * 0.9 / (d1 / 2 + (v104 - v101).Y / 2)));
            Vector2 v110 = Vector2.Polar(v100, (v0 - v7).Y, pi / 2 - (2 * pi / arrayCount / 2) - Math.Asin(d3s * 0.9 / (v0 - v7).Y));
            Vector2 v111 = Vector2.Polar(v100, d3 / 2, pi / 2 - (2 * pi / arrayCount / 2) - Math.Asin(d3s * 0.9 / (d3 / 2)));
            Vector2 v112 = Vector2.Polar(v100, d1 / 2, pi / 2 - (2 * pi / arrayCount) + Math.Asin(d3s * 0.9 / (d1 / 2)));
            Vector2 v113 = Vector2.Polar(v100, (v0 - v7).Y, pi / 2 - (2 * pi / arrayCount) + Math.Asin(d3s * 0.9 / (v0 - v7).Y));
            Vector2 v114 = Vector2.Polar(v100, d3 / 2, pi / 2 - (2 * pi / arrayCount) + Math.Asin(d3s * 0.9 / (d3 / 2)));

            #endregion

            #region 线

            //右图
            Line linec1 = new Line(fun.Polar2(v0, 2, 0), fun.Polar2(v0, l1 + 2, 180));
            Line linec2 = new Line(v31, fun.Polar2(v31, d2, 90));

            LwPolyline lw1 = Pus.AddPointsToLwPolyLine(v0, v7, v13, v21, new Vector2(v0.X - l4, v0.Y - d5 / 2), new Vector2(v0.X - l4 - l3, v0.Y - d5 / 2), v30, v40, fun.Polar2(v0, l1, 180), v36, v1);
            LwPolyline lw2 = Pus.AddPointsToLwPolyLine(v21, v14, v22, v30);
            LwPolyline lw3 = Pus.AddPointsToLwPolyLine(v8, v9, v16, v17, v10, v11, v19, v20, v12, v13);
            LwPolyline lw4 = Pus.AddPointsToLwPolyLine(v3, v9, v16, v15, v23, v24, v34, v35, v25, v24, v16, v17, v10, v4);
            LwPolyline lw5 = Pus.AddPointsToLwPolyLine(v5, v11, v19, v18, v26, v28, v38, v39, v29, v27, v19, v20, v12, v6);
            LwPolyline lw6 = Pus.AddPointsToLwPolyLine(v2, v32, v33, v37);
            LwPolyline lw7 = Pus.AddPointsToLwPolyLine(fun.Polar2(v33, d4 / 2, 90), v33, v32, fun.Polar2(v32, d1 / 2, 90));

            LwPolyline lwm1 = Pus.MirrorPolyline(lw1, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm2 = Pus.MirrorPolyline(lw2, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm3 = Pus.MirrorPolyline(lw3, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm4 = Pus.MirrorPolyline(lw4, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm5 = Pus.MirrorPolyline(lw5, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm6 = Pus.MirrorPolyline(lw6, v0, fun.Polar2(v0, l1, 180));
            LwPolyline lwm7 = Pus.MirrorPolyline(lw7, v0, fun.Polar2(v0, l1, 180));

            //左图
            Line linec11 = new Line(fun.Polar2(v100, d3 / 2 + 2, 180), fun.Polar2(v100, d3 / 2 + 2, 0));
            Line linec12 = fun.RotateLine(linec11, v100, 90);

            LwPolyline lw11 = Pus.AddPointsToLwPolyLine(v101, v112, v114, v111, v109, v106, v108, v105, v101);
            LwPolyline lw12 = Pus.AddPointsToLwPolyLine(v113, v110, v109, v106, v107, v104, v103, fun.Polar2(v103, d3s * 1.8, 180), v103, v102, fun.Polar2(v102, d3s * 1.8, 180));
            lw11.Vertexes[0].Bulge = -Math.Tan((Vector2.Angle(v100, v101) - Vector2.Angle(v100, v112) + ((colst == "B") ? 0 : (2 * pi))) / 4);
            lw11.Vertexes[2].Bulge = Math.Tan((Vector2.Angle(v100, v105) - Vector2.Angle(v100, v108)) / 4);
            lw11.Vertexes[6].Bulge = Math.Tan((Vector2.Angle(v100, v105) - Vector2.Angle(v100, v108)) / 4);
            lw12.Vertexes[0].Bulge = Math.Tan((Vector2.Angle(v100, v105) - Vector2.Angle(v100, v108)) / 4);
            lw12.Vertexes[4].Bulge = Math.Tan((Vector2.Angle(v100, v105) - Vector2.Angle(v100, v108)) / 4);

            LwPolyline[] lws11 = fun.CircularArrayLwPolyline(lw11, v100, arrayCount);
            LwPolyline[] lws12 = fun.CircularArrayLwPolyline(lw12, v100, arrayCount);

            #endregion

            #region 标注
            Vector2 vd1 = new Vector2(v0.X - l4, v0.Y - d5 / 2);

            fun.DimensionLiner(dxf, dimensioning, v2, fun.Polar2(v2, d1, 90), -offset, 90, "DCONWS=%%c" + d1 / scale);
            fun.DimensionLiner(dxf, dimensioning, v31, fun.Polar2(v31, d2, 90), offset, 90, "DFC=%%c" + d2 / scale);
            fun.DimensionLiner(dxf, dimensioning, v13, fun.Polar2(v13, d3, 90), offset * 3 + v0.X - v13.X, -90, "BD=%%c" + d3 / scale);
            fun.DimensionLiner(dxf, dimensioning, v37, fun.Polar2(v37, d4, 90), offset, 90, "DCOB=%%c" + d4 / scale);
            fun.DimensionLiner(dxf, dimensioning, vd1, fun.Polar2(vd1, d5, 90), offset * 2 + v0.X - v20.X, -90, "DGUI=%%c" + d5 / scale);
            //fun.DimensionLiner(dxf, dimensioning, v21, fun.Polar2(v21, d6, 90), offset * 4 + v0.X - v21.X, -90, "BD=%%c" + d6 / scale);

            fun.DimensionLiner(dxf, dimensioning, v40, v7, offset * 3 + (v40 + v7).Y / 2 - v30.Y, 180, "LCOL=" + l1 / scale);
            fun.DimensionLiner(dxf, dimensioning, v31, v21, offset * 2 + (v31 + v21).Y / 2 - v30.Y, 180, "LDG=" + l2 / scale);
            fun.DimensionLiner(dxf, dimensioning, v30, v21, -(offset + (v30 + v21).Y / 2 - v30.Y), 0, "GW=" + l3 / scale);
            fun.DimensionLiner(dxf, dimensioning, v21, v7, -(offset * 2 + (v21 + v7).Y / 2 - v30.Y), 0, "LFSF=" + l4 / scale);
            fun.DimensionLiner(dxf, dimensioning, v37, v33, offset * 2 + (v37 + v33).Y / 2 - v30.Y, 180, "LCOB=" + l5 / scale);


            if (ang == Math.Atan(1.0 / 10))
                fun.DimensionTaper(dxf, Vector2.MidPoint(v30, Vector2.MidPoint(v30, v40)), -135, offset, offset, "1:10");

            if (ang == 8 * rad)
                fun.DimensionAngular(dxf, dimensioning, new Line(v30, v40), new Line(v30, fun.Polar2(v30, 1, 180)), offset * 4, ang * 180 / pi + "°");

            #endregion

            #region 图框

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            #endregion

            #region 编辑

            fun.AddEntities(dxf, center, linec1, linec2);
            fun.AddEntities(dxf, contour, lw1, lw2, lw3, lw4, lwm1, lwm2, lwm3, lwm4);
            fun.AddEntities(dxf, refin, lw6, lw7, lwm6, lwm7);
            if (arrayCount == 6)
                fun.AddEntities(dxf, contour, lw5, lwm5);

            fun.AddEntities(dxf, center, linec11, linec12);
            fun.AddEntities(dxf, contour, lws11);
            fun.AddEntities(dxf, contour, lws12);

            fun.SetTexts(dxf);

            #endregion

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        public static void ToolShanksColletSeats(Dictionary<string, Object> data)//夹头座
        {
            DxfDocument dxf = new DxfDocument();

            #region 图层

            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层

            #endregion

            #region 参数

            double pi = Math.PI;
            double rad = pi / 180;
            double offset = 10;

            string path = Convert.ToString(data["path"]);
            double scale = Convert.ToDouble(data["scale"]);
            //double scale = 2;

            /* double d2 = 64.72 * scale;
             double d7 = 81.74 * scale;
             double d8 = 47.74 * scale;

             double l6 = 33.36 * scale;
             double l7 = 111 * scale;

             double ang = Math.Atan(1.0 / 10);
             //double ang = 8 * rad;*/

            double d2 = Convert.ToDouble(data["dfc"]) * scale;
            double d7 = Convert.ToDouble(data["thszws"]) * scale;
            double d8 = Convert.ToDouble(data["dtan"]) * scale;

            double l6 = Convert.ToDouble(data["lb"]) * scale;
            double l7 = Convert.ToDouble(data["oal"]) * scale;

            string ta = Convert.ToString(data["ta"]);//锥度

            double ang = (ta == "0") ? 8 * rad : Math.Atan(1.0 / 10);

            #endregion

            #region 点

            Vector2 v0 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(v0.X, v0.Y - d2 / 2);
            Vector2 v2 = new Vector2(v0.X, v0.Y - d7 / 2 + 1 * scale);
            Vector2 v3 = new Vector2(v0.X - 1 * scale, v0.Y - d7 / 2);
            Vector2 v4 = new Vector2(v2.X - l6, v2.Y);
            Vector2 v5 = new Vector2(v4.X, v3.Y);
            Vector2 v6 = new Vector2(v0.X - (d2 - d8) / 2 / Math.Tan(ang), v0.Y - d8 / 2);
            Vector2 v7 = new Vector2(v0.X - l7, v0.Y - d8 / 4);
            Vector2 v8 = new Vector2(v0.X - l7, v0.Y - d8 / 2);
            Vector2 v9 = new Vector2(v0.X - l7 * 1.2, v0.Y);
            Vector2 v10 = new Vector2(v9.X, v7.Y);
            Vector2 v11 = new Vector2(v10.X, v3.Y);

            #endregion

            #region 线

            Line linec1 = new Line(fun.Polar2(v0, 2, 0), fun.Polar2(v0, l7 * 1.2 + 2, 180));

            LwPolyline lw1 = Pus.AddPointsToLwPolyLine(v10, v7, fun.Polar2(v0, l7, 180), v8, v6, fun.Polar2(v6, d8 / 2, 90), v6, v1, v0, v1, v2, v3, v5, v11);
            LwPolyline lw2 = Pus.AddPointsToLwPolyLine(v2, v4, v5);
            LwPolyline lw3 = Pus.AddPointsToLwPolyLine(v9, v10, v11);
            LwPolyline lwh1 = Pus.AddPointsToLwPolyLine((LwPolyline)lw1.Clone(), v10);

            LwPolyline lwm1 = Pus.MirrorPolyline(lw1, v0, fun.Polar2(v0, l7, 180));
            LwPolyline lwm2 = Pus.MirrorPolyline(lw2, v0, fun.Polar2(v0, l7, 180));
            LwPolyline lwm3 = Pus.MirrorPolyline(lw3, v0, fun.Polar2(v0, l7, 180));
            LwPolyline lwhm1 = Pus.MirrorPolyline(lwh1, v0, fun.Polar2(v0, l7, 180));

            lw3.Vertexes[0].Bulge = -Math.Tan(pi / 16);
            lw3.Vertexes[1].Bulge = Math.Tan(pi / 16);
            lwh1.Vertexes[lwh1.Vertexes.Count - 2].Bulge = -Math.Tan(pi / 16);

            lwm3.Vertexes[0].Bulge = -Math.Tan(pi / 16);
            lwm3.Vertexes[1].Bulge = Math.Tan(pi / 16);
            lwhm1.Vertexes[lwh1.Vertexes.Count - 2].Bulge = -Math.Tan(pi / 16);


            fun.AddHatch(dxf, hatching, lwh1, new Line[32], new Arc[32], 45, offset);
            fun.AddHatch(dxf, hatching, lwhm1, new Line[32], new Arc[32], 45, offset);

            #endregion

            #region 标注

            fun.DimensionLiner(dxf, dimensioning, v1, fun.Polar2(v1, d2, 90), offset * 2, -90, "DFC=%%c" + d2 / scale);
            fun.DimensionLiner(dxf, dimensioning, v3, fun.Polar2(v3, d7, 90), offset * 3 + v0.X - v3.X, -90, "THSZWS=%%c" + d7 / scale);
            fun.DimensionLiner(dxf, dimensioning, v6, fun.Polar2(v6, d8, 90), -(offset + v0.X - v6.X), 90, "DTAN=%%c" + d8 / scale);

            fun.DimensionLiner(dxf, dimensioning, v2, v4, offset + (v2 + v4).Y / 2 - v3.Y, 180, "LB=" + l6 / scale);
            fun.DimensionLiner(dxf, dimensioning, v2, v8, offset * 2 + (v2 + v8).Y / 2 - v3.Y, 180, "OAL=" + l7 / scale);

            if (ang == Math.Atan(1.0 / 10))
                fun.DimensionTaper(dxf, Vector2.MidPoint(v1, v6), 135, offset, -offset, "1:10");
            if (ang == 8 * rad)
                fun.DimensionAngular(dxf, dimensioning, new Line(v6, v1), new Line(v6, fun.Polar2(v6, 1, 0)), offset * 4, ang * 180 / pi + "°");

            #endregion

            #region 图框

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            #endregion

            #region 编辑

            fun.AddEntities(dxf, center, linec1);
            fun.AddEntities(dxf, contour, lw1, lwm1);
            fun.AddEntities(dxf, additionalline, lw2, lw3, lwm2, lwm3);

            fun.SetTexts(dxf);

            #endregion

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        public static void ToolShanksNuts(Dictionary<string, Object> data)//螺母
        {
            DxfDocument dxf = new DxfDocument();

            #region 图层

            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层

            #endregion

            #region 参数

            string path = Convert.ToString(data["path"]);
            double scale = Convert.ToDouble(data["scale"]);
            //double scale = 0.5;

            double pi = Math.PI;
            double rad = pi / 180;
            double offset = 10;

            /*double d7 = 72.35 * scale;
            double d9 = 101.06 * scale;
            double d10 = 70.17 * scale;

            double l8 = 44.41 * scale;
            double l9 = 15.76 * scale;
            double l10 = 6.5 * scale;
            double l11 = 31.39 * scale;*/

            double d7 = Convert.ToDouble(data["thszws"]) * scale;
            double d9 = Convert.ToDouble(data["d9"]) * scale;
            double d10 = Convert.ToDouble(data["d10"]) * scale;

            double l8 = Convert.ToDouble(data["l8"]) * scale;
            double l9 = Convert.ToDouble(data["l9"]) * scale;
            double l10 = Convert.ToDouble(data["l10"]) * scale;
            double l11 = Convert.ToDouble(data["l11"]) * scale;

            double l9s = l9 / 15.76;

            #endregion

            #region 点

            Vector2 v0 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(v0.X, v0.Y + d10 / 2 - l9s * 12.69 * Math.Tan(30 * rad));
            Vector2 v2 = new Vector2(v0.X, v0.Y + d9 / 2 - l9s * 12.69 * Math.Tan(45 * rad));
            Vector2 v3 = new Vector2(v0.X - l9s * 12.69, v0.Y);
            Vector2 v4 = new Vector2(v3.X, v0.Y + l11);
            Vector2 v5 = new Vector2(v3.X, v0.Y + d10 / 2);
            Vector2 v6 = new Vector2(v5.X, v0.Y + d9 / 2);
            Vector2 v7 = new Vector2(v0.X - l9, v4.Y);
            Vector2 v8 = new Vector2(v7.X, v0.Y + d10 / 2 + (d9 - d10) / 2 / 2);
            Vector2 v9 = new Vector2(v0.X - l9 - l10, v0.Y);
            Vector2 v10 = new Vector2(v9.X, v0.Y + d7 / 2 - 1 * scale);
            Vector2 v11 = new Vector2(v9.X, v0.Y + d7 / 2);
            Vector2 v12 = new Vector2(v9.X, v8.Y);
            Vector2 v13 = new Vector2(v0.X - l8 + 1 * scale, v0.Y);
            Vector2 v14 = new Vector2(v13.X, v10.Y);
            Vector2 v15 = new Vector2(v0.X - l8, v0.Y);
            Vector2 v16 = new Vector2(v15.X, v11.Y);
            Vector2 v17 = new Vector2(v15.X, v6.Y);

            #endregion

            #region 线

            Line linec1 = new Line(fun.Polar2(v0, 2, 0), fun.Polar2(v0, l8 + 2, 180));
            Line line1 = new Line(v11, v16);
            Line linem1 = fun.mirrorline(line1, v0, v3);

            LwPolyline lw1 = Pus.AddPointsToLwPolyLine(v5, v1, v0, v2, v6, v17, v15, v16, v14, v13, v14, v10, v9, v12);
            LwPolyline lw2 = Pus.AddPointsToLwPolyLine(v12, v8, v7, v4, v5, new Vector2(v5.X, (v0 - v12).Y), new Vector2(v12.X, (v0 - v12).Y), v12);
            LwPolyline lwm1 = Pus.MirrorPolyline(lw1, v0, v3);

            LwPolyline lwh1 = Pus.AddPointsToLwPolyLine(v1, v2, v6, v17, v16, v14, v10, v12, v8, v7, v4, v5, v1);
            LwPolyline lwh2 = Pus.MirrorPolyline(Pus.AddPointsToLwPolyLine(v1, v2, v6, v17, v16, v14, v10, v12, new Vector2(v4.X, v12.Y), v5, v1), v0, v3);

            fun.AddHatch(dxf, hatching, lwh1, new Line[32], new Arc[32], 45, offset);
            fun.AddHatch(dxf, hatching, lwh2, new Line[32], new Arc[32], 45, offset);

            #endregion

            #region 标注

            fun.DimensionLiner(dxf, dimensioning, v16, fun.Polar2(v16, d7, -90), offset * 2, 90, "THSZWS=%%c" + d7 / scale);
            fun.DimensionLiner(dxf, dimensioning, v6, fun.Polar2(v6, d9, -90), -(offset * 2 + v0.X - v6.X), 90, "D9=%%c" + d9 / scale);
            fun.DimensionLiner(dxf, dimensioning, v5, fun.Polar2(v5, d10, -90), -(offset + v0.X - v5.X), 90, "D10=%%c" + d10 / scale);

            fun.DimensionLiner(dxf, dimensioning, new Vector2(v2.X, (v0 - v2).Y), new Vector2(v17.X, (v0 - v17).Y), -(offset + (v17 - v2).Y / 2), 0, "L8=" + l8 / scale);
            fun.DimensionLiner(dxf, dimensioning, v2, v8, offset + v6.Y - (v8 + v2).Y / 2, 0, "L9=" + l9 / scale);
            fun.DimensionLiner(dxf, dimensioning, v8, v12, -(offset + v6.Y - v8.Y), 180, "L10=" + l10 / scale);
            fun.DimensionLiner(dxf, dimensioning, v3, v4, offset + v3.X - v15.X, 90, "L11=" + l11 / scale);


            #endregion

            #region 图框

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            #endregion

            #region 编辑

            fun.AddEntities(dxf, center, linec1);
            fun.AddEntities(dxf, contour, lw1, lw2, lwm1);
            fun.AddEntities(dxf, additionalline, line1, linem1);

            #endregion

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

    }
}
